package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;
import com.nispok.snackbar.enums.SnackbarType;
import com.nispok.snackbar.listeners.ActionClickListener;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.MobileChart;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.holder.EmptyHolder;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.listener.PieChartOnValueSelectListener;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;

/**
 * Created by hacker on 2017/7/18.
 */

public class InOutDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    List<MobileChart> mDatas;
    private Context mContext;
    private int type;

    public InOutDataAdapter(List<MobileChart> datas, Context mContext,int type) {
        this.mDatas = datas;
        this.mContext = mContext;
        this.type=type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.today_data_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas == null || mDatas.isEmpty()) {
            return TYPE_EMPTY;
        } else {
            return TYPE_DEFAULT;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            Object obj = mDatas.get(position);
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.bindViewData((MobileChart) obj);
            viewHolder.setViewListener();
        }
    }

    @Override
    public int getItemCount() {
        if (mDatas != null && mDatas.size() > 0) {
            return mDatas.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_EMPTY:
                return 0;
            default:
                return 1;
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        PieChartView pieChartView;
        PieChartView pieChartViewWeight;

        public ViewHolder(View itemView) {
            super(itemView);
            pieChartView = (PieChartView) itemView.findViewById(R.id.pieChartView);
            pieChartViewWeight = (PieChartView) itemView.findViewById(R.id.pieChartViewWeight);
        }

        public void bindViewData(MobileChart chart) {
            List<SliceValue> values = new ArrayList<>();

            SliceValue value = new SliceValue();
            value.setValue(chart.getCars());
            value.setColor(mContext.getResources().getColor(R.color.recycler_color1));
            value.setLabel(chart.getCars()+ " 车");
            values.add(value);

            SliceValue value1 = new SliceValue();
            value1.setValue(chart.getCoals());
            value1.setColor(mContext.getResources().getColor(R.color.recycler_color2));
            value1.setLabel(chart.getCoals()+ " 个煤种");
            values.add(value1);

            SliceValue value2 = new SliceValue();
            value2.setValue(chart.getCustomers());
            value2.setColor(mContext.getResources().getColor(R.color.recycler_color3));
            if (type == Constant.TYPE_IN){
                value2.setLabel(chart.getCustomers() + " 家供应商");
            }else {
                value2.setLabel(chart.getCustomers() + " 客户");
            }
            values.add(value2);


            PieChartData data = new PieChartData();
            data.setValues(values);
            data.setHasLabelsOutside(false);
            data.setHasCenterCircle(true);
            data.setCenterCircleColor(mContext.getResources().getColor(R.color.colorPrimary));
            data.setCenterText1("数量");
            data.setCenterText2(chart.getTimeTag());
            data.setCenterText1FontSize(16);
            data.setCenterText2FontSize(12);
            data.setSlicesSpacing(1);

            pieChartView.setPieChartData(data);
            pieChartView.setCircleFillRatio(1);
            pieChartView.setChartRotation(45, true);
            pieChartView.setChartRotationEnabled(false);

            List<SliceValue> values2 = new ArrayList<>();

            SliceValue value4 = new SliceValue();
            value4.setValue(chart.getGrossWeight());
            value4.setColor(mContext.getResources().getColor(R.color.recycler_color1));
            value4.setLabel("毛重："+chart.getGrossWeight()+ "吨");
            values2.add(value4);

            SliceValue value5 = new SliceValue();
            value5.setValue(chart.getTraeWeight());
            value5.setColor(mContext.getResources().getColor(R.color.recycler_color2));
            value5.setLabel("皮重："+chart.getTraeWeight()+ "吨");
            values2.add(value5);
            SliceValue value6 = new SliceValue();
            value6.setValue(chart.getNetWeight());
            value6.setColor(mContext.getResources().getColor(R.color.recycler_color3));
            value6.setLabel("净重："+chart.getNetWeight()+ "吨");
            values2.add(value6);
            SliceValue value7 = new SliceValue();
            value7.setValue(chart.getNetWeight());
            value7.setColor(mContext.getResources().getColor(R.color.recycler_color4));
            value7.setLabel("磅差："+chart.getLossWeight()+ "吨");
            values2.add(value7);

            PieChartData data2 = new PieChartData();
            data2.setValues(values2);
            data2.setHasLabelsOutside(true);
            data2.setHasCenterCircle(true);
            data2.setCenterCircleColor(mContext.getResources().getColor(R.color.colorPrimary));
            data2.setCenterText1("吨位");
            data2.setCenterText2(chart.getTimeTag());
            data2.setCenterText1FontSize(16);
            data2.setCenterText2FontSize(12);
            data2.setSlicesSpacing(1);

            pieChartViewWeight.setPieChartData(data2);
            pieChartViewWeight.setCircleFillRatio(1);
            pieChartViewWeight.setChartRotation(45, true);
            pieChartViewWeight.setChartRotationEnabled(false);
        }

        public void setViewListener() {
            pieChartView.setOnValueTouchListener(new PieChartOnValueSelectListener() {
                @Override
                public void onValueSelected(int arcIndex, final SliceValue value) {

                    com.nispok.snackbar.Snackbar snackbar =
                            com.nispok.snackbar.Snackbar
                                    .with(mContext)
                                    .type(SnackbarType.MULTI_LINE)
                                    .duration(com.nispok.snackbar.Snackbar.SnackbarDuration.LENGTH_SHORT)
                                    .position(com.nispok.snackbar.Snackbar.SnackbarPosition.BOTTOM)
                                    .margin(15, 15)
                                    .backgroundDrawable(R.color.colorPrimary)
                                    .text(String.valueOf(value.getLabel()))
                                    .textColor(Color.WHITE)
                                    .actionLabel("查看")
                                    .actionColor(Color.WHITE)
                                    .actionListener(new ActionClickListener() {
                                        @Override
                                        public void onActionClicked(Snackbar snackbar) {
                                            Toast.makeText(mContext, String.valueOf(value.getLabel()), Toast.LENGTH_SHORT).show();
                                        }
                                    });

                    SnackbarManager.show(snackbar);
                }

                @Override
                public void onValueDeselected() {

                }
            });
            pieChartViewWeight.setOnValueTouchListener(new PieChartOnValueSelectListener() {
                @Override
                public void onValueSelected(int arcIndex, final SliceValue value) {

                    com.nispok.snackbar.Snackbar snackbar =
                            com.nispok.snackbar.Snackbar
                                    .with(mContext)
                                    .type(SnackbarType.MULTI_LINE)
                                    .duration(com.nispok.snackbar.Snackbar.SnackbarDuration.LENGTH_SHORT)
                                    .position(com.nispok.snackbar.Snackbar.SnackbarPosition.BOTTOM)
                                    .margin(15, 15)
                                    .backgroundDrawable(R.color.colorPrimary)
                                    .text(String.valueOf(value.getLabel()))
                                    .textColor(Color.WHITE)
                                    .actionLabel("查看")
                                    .actionColor(Color.WHITE)
                                    .actionListener(new ActionClickListener() {
                                        @Override
                                        public void onActionClicked(Snackbar snackbar) {
                                            Toast.makeText(mContext, String.valueOf(value.getLabel()), Toast.LENGTH_SHORT).show();
                                        }
                                    });

                    SnackbarManager.show(snackbar);
                }

                @Override
                public void onValueDeselected() {

                }
            });
        }
    }

}
